# -*- coding:utf-8 -*-
from flask import abort
from flask import g
from flask import json
from flask import render_template
from flask import request
from flask_login import login_required
from sqlalchemy import desc

from app.actions.menu import menu
from app.database.models import Menu

__author__ = 'JiangW'


@menu.route('/', methods=['GET'])
def menu_list():
    query_dict = {
        'is_active': True,
    }

    page = request.args.get('page', 1, type=int)
    pagination = Menu.query.filter_by(**query_dict).order_by(desc(Menu.update_on)).paginate(
        page, per_page=20, error_out=False)
    return render_template('menu/menu_list.html', pagination=pagination)


@menu.route('/edit', methods=['GET', 'POST'])
@login_required
def menu_create():
    if request.method == 'GET':
        return render_template('menu/menu_edit.html')
    else:
        res = {'code': 1000}
        menu_name = request.values.get('menu_name')
        menu_code = request.values.get('menu_code')
        menu_position = request.values.get('menu_position')
        menu_link_url = request.values.get('menu_link_url')
        menu_is_admin = request.values.get('menu_is_admin')
        menu_icon_class = request.values.get('menu_icon_class')
        if menu_name and menu_code and menu_link_url:
            create_menu = Menu(title=menu_name, code=menu_code, link_url=menu_link_url, user_id=g.default_user.id)
            if menu_icon_class:
                create_menu.icon_class = menu_icon_class
            if menu_position:
                create_menu.position = int(menu_position)
            if menu_is_admin:
                create_menu.is_admin = True if menu_is_admin == '1' else False
            create_menu.save()
        else:
            res['code'] = 1001
        return json.dumps(res)


@menu.route('/edit/<int:menu_id>', methods=['GET', 'POST'])
@login_required
def menu_edit(menu_id):
    current_menu = Menu.query.get(int(menu_id))
    if current_menu:
        if request.method == 'GET':
            return render_template('menu/menu_edit.html', current_menu=current_menu)
        else:
            res = {'code': 1000}
            menu_name = request.values.get('menu_name')
            menu_code = request.values.get('menu_code')
            menu_position = request.values.get('menu_position')
            menu_link_url = request.values.get('menu_link_url')
            menu_is_admin = request.values.get('menu_is_admin')
            menu_icon_class = request.values.get('menu_icon_class')
            if menu_name and menu_code and menu_link_url:
                current_menu.title = menu_name
                current_menu.code = menu_code
                current_menu.link_url = menu_link_url
                if menu_icon_class:
                    current_menu.icon_class = menu_icon_class
                if menu_position:
                    current_menu.position = int(menu_position)
                if menu_is_admin:
                    current_menu.is_admin = True if menu_is_admin == '1' else False
                current_menu.save()
            else:
                res['code'] = 1001
            return json.dumps(res)
    else:
        return abort(404)


@menu.route('/delete/<int:menu_id>', methods=['GET', 'POST'])
@login_required
def menu_delete(menu_id):
    current_menu = Menu.query.get(int(menu_id))
    if current_menu:
        if request.method == 'GET':
            return render_template('menu/menu_delete.html', current_menu=current_menu)
        else:
            res = {'code': 1000}
            current_menu.delete()
            return json.dumps(res)
    else:
        return abort(404)